﻿--[[
   @brief    WGSelectZoneList(区服下拉菜单)
   @author   harleyxwang
   @version  1.0
   @date     2017-04-27
--]]



ierd_tgp.tpf_ui.Require("frame_bottom\\select_zone.lua")

WGSelectZoneList = {

    MAX_SHOW = 3;  -- 最多显示多少个
    kShadeTop = 0;  -- 顶部阴影高度
    kShadeWidth = 0;  -- 左右阴影宽度
    kExpectSpace = 0;  -- 期望与Banner底部的间隔
    kListSpaceForPlugin = -2;
    kListXOffsetForPlugin = 2;
    color_mode_ = MainCommon.color_mode.kDark;
    meunu_id = 0;
    light_meunu_id = 0;
    dark_meunu_id = 0;
    current_game_id = 0;
    menue_id_to_zone_id = {};
    LIST_TYPE = {
        FOR_MINI_MODE = 0,
        FOR_PLUGIN_PAGE = 1,
    };
    menu_item_hover_type = {};
    menu_item_hover_dark_type = {
        hover_up = "ONEIMAGE:banner_menu\\menu_item_hover.png;Stretch;0;";
        hover_mid = "ONEIMAGE:banner_menu\\menu_item_hover.png;Stretch;0;";
        hover_down = "ONEIMAGE:banner_menu\\menu_item_hover.png;Stretch;0;";
        hover_updown = "ONEIMAGE:banner_menu\\menu_item_hover.png;Stretch;0;";
    };

    menu_item_hover_light_type = {
        hover_up = "9IMAGES:bottom_bar\\bottom_bar.png?379,130,387,138;bottom_bar\\bottom_bar.png?389,130,397,138;bottom_bar\\bottom_bar.png?399,130,407,138;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;Stretch;0;";
        hover_mid = "9IMAGES:bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;Stretch;0;";
        hover_down = "9IMAGES:bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,150,387,158;bottom_bar\\bottom_bar.png?389,150,397,158;bottom_bar\\bottom_bar.png?399,150,407,158;Stretch;null;";
        hover_updown = "9IMAGES:bottom_bar\\bottom_bar.png?379,130,387,138;bottom_bar\\bottom_bar.png?389,130,397,138;bottom_bar\\bottom_bar.png?399,130,407,138;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,150,387,158;bottom_bar\\bottom_bar.png?389,150,397,158;bottom_bar\\bottom_bar.png?399,150,407,158;Stretch;0;"; 
    };

    list_type = 0;
    other_item_id = nil;
    init = function(list_type)
        ierd_tgp.tpf_ui.Log("[WGSelectZoneList]init");

        local main_uilib = ui_utility.uilib();
        local main_wnd_id = main_uilib:GetControlIDbyCustomID(CID_MAINFRAME_Dialog_MainFrame);
        if main_wnd_id ~= 0 then
            local const_ui_str, str_holder = U8toCUIStr("WinMenu_ZoneList");
            WGSelectZoneList.list_type = list_type;

            if WGSelectZoneList.LIST_TYPE.FOR_PLUGIN_PAGE == list_type then
                local const_ui_str2, str_holder2 = U8toCUIStr("WinMenu_ZoneList_For_Plugin");
                WGSelectZoneList.dark_meunu_id = main_uilib:GetControlIDbyPath(const_ui_str2, main_wnd_id);
                local const_ui_str3, str_holder3 = U8toCUIStr("WinMenu_LightZoneList_For_Plugin");
                WGSelectZoneList.light_meunu_id = main_uilib:GetControlIDbyPath(const_ui_str3, main_wnd_id);
            else
                WGSelectZoneList.dark_meunu_id = main_uilib:GetControlIDbyPath(const_ui_str, main_wnd_id);
            end
            local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
            if color_data_config ~= nil and color_data_config.color_mode ~=nil then
                WGSelectZoneList.color_mode_ = color_data_config.color_mode;
            end;

            WGSelectZoneList.meunu_id = WGSelectZoneList.dark_meunu_id;
            WGSelectZoneList.menu_item_hover_type = WGSelectZoneList.menu_item_hover_dark_type;
            if WGSelectZoneList.color_mode_ == MainCommon.color_mode.kLight and
               WGSelectZoneList.LIST_TYPE.FOR_PLUGIN_PAGE == list_type then
                WGSelectZoneList.meunu_id = WGSelectZoneList.light_meunu_id;
                WGSelectZoneList.menu_item_hover_type = WGSelectZoneList.menu_item_hover_light_type;
            end
            WGSelectZoneList.init_menue_item();
        else
            ierd_tgp.tpf_ui.Log("[WGSelectZoneList][init]main_wnd_id is INVALID", true);
        end
    end;
        
    --[[
    @brief: 输入参数为banner上菜单按钮相对屏幕的位置
    ]]
    show = function(xoffset, yoffset, game_id)
        base_utility.log("[WGSelectZoneList][show]x:" .. tostring(xoffset) .. ", y:" .. tostring(yoffset));

        local color_data_config = comm_center.call_service("SvrTGPConfigMgr_GetColorMode", {});
        if color_data_config ~= nil and color_data_config.color_mode ~=nil then
            WGSelectZoneList.color_mode_ = color_data_config.color_mode;
        end;

        WGSelectZoneList.meunu_id = WGSelectZoneList.dark_meunu_id;
        WGSelectZoneList.menu_item_hover_type = WGSelectZoneList.menu_item_hover_dark_type;
        if WGSelectZoneList.color_mode_ == MainCommon.color_mode.kLight then
            WGSelectZoneList.meunu_id = WGSelectZoneList.light_meunu_id;
            WGSelectZoneList.menu_item_hover_type = WGSelectZoneList.menu_item_hover_light_type;
        end

        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(WGSelectZoneList.meunu_id);
        if nil == pMenuControl then
            base_utility.log("[WGSelectZoneList][hide]pMenuControl is NIL", true);
            return;
        end
        
        --根据游戏设置菜单项
        WGSelectZoneList.current_game_id = game_id;

        WGSelectZoneList.init_menue_item();
        
        WGSelectZoneList.refresh_list(game_id);
        
        local rc = pMenuControl:GetRectEx(TRUE,TRUE);
        if WGSelectZoneList.LIST_TYPE.FOR_PLUGIN_PAGE == WGSelectZoneList.list_type then
            local h = rc.bottom - rc.top;
            yoffset = yoffset - h + WGSelectZoneList.kListSpaceForPlugin;
            xoffset = xoffset + WGSelectZoneList.kListXOffsetForPlugin;
        end
        local new_rc = WGSelectZoneList.move_rc(xoffset, yoffset, rc);
        
        pMenuControl:MoveEx(new_rc,TRUE);
        pMenuControl:BringToTop();
        pMenuControl:Show(TRUE);
    end;

    fire_zone_list_show = function(show)
        local params = {};
        params.show = show;
        params.game_id = WGSelectZoneList.current_game_id;
        event_center.send_event(banner_event.ZONE_LIST_SHOW, base_utility.table_to_json(params));
    end;
    
    move_rc = function(xoffset, yoffset, rc)        
        --获取主窗口的位置
        local main_wnd = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        if nil == main_wnd then
            base_utility.log("[WGSelectZoneList][move]main_wnd is NIL", true);
            return rc;
        end
        
        local main_wnd_rc = main_wnd:GetRectEx(TRUE,TRUE);

        local w = rc.right - rc.left;
        local h = rc.bottom - rc.top;

        rc.left = xoffset - main_wnd_rc.left - WGSelectZoneList.kShadeWidth;
        rc.right = rc.left + w;
        rc.top = yoffset - main_wnd_rc.top - WGSelectZoneList.kShadeTop +
            WGSelectZoneList.kExpectSpace;
        rc.bottom = rc.top + h;
        
        return rc;
    end;

    hide = function()
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(WGSelectZoneList.meunu_id);
        if nil == pMenuControl then
            return;
        end
        
        if pMenuControl ~= nil then
            pMenuControl:Show(FALSE);
        end
    end;
    
    init_menue_item = function()
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(WGSelectZoneList.meunu_id);
        if nil == pMenuControl then
            base_utility.log("[WGSelectZoneList][init_menue_item]pMenuControl is NIL", true);
            return;
        end
        
        pMenuControl:RemoveAll();

        WGSelectZoneList.menue_id_to_zone_id = {};
    end;
    
    add_space_item = function()
    end;
    
    add_other_zone_item = function(game_id, is_init)
        local item_id = WGSelectZoneList.add_zone_item_impl();
        if item_id == Tenio.INVALID_CONTROLID then
            base_utility.log("[WGSelectZoneList][add_zone_item]item id is INVALID_CONTROLID", true);
            return item_id;
        end        
        
        -- 显示其他大区选项
        -- MainCommon.show_only_child_ex(item_id, "TxtOtherZone");
        ui_utility.show_only_child(item_id, "TxtOtherZone");
    
        -- TODO(amadeusyu):MC显示为“其他版本”，建议后续增加类型区分字段
        local other_text = LoadStr("FRAME_BOTTOM_OTHER_ZONE");
        if is_init == true then
            other_text = LoadStr("FRAME_BOTTOM_OTHER_ZONE_INIT");
        end;
        if game_id == GAME_ID.GAME_ID_MC then
            other_text = LoadStr("FRAME_BOTTOM_OTHER_VERSION");
        end
        -- MainCommon.set_static_text_ex(item_id, "TxtOtherZone", other_text);
        ui_utility.set_static_text(item_id, "TxtOtherZone", other_text);
        return item_id;
    end;
    
    add_zone_item = function(game_id, server_info)
        if server_info == nil then
            base_utility.log("[WGSelectZoneList][add_zone_item]server_info is NIL", true);
            return Tenio.INVALID_CONTROLID;
        end
        
        local item_id = WGSelectZoneList.add_zone_item_impl(server_info.id);
        if item_id == Tenio.INVALID_CONTROLID then
            base_utility.log("[WGSelectZoneList][add_zone_item]item id is INVALID_CONTROLID", true);
            return item_id;
        end        
        
        -- MainCommon.show_only_child_ex(item_id, "UobContent");
        ui_utility.show_only_child(item_id, "UobContent");
        local split_line_ptr = ui_utility.get_ctrl_ptr_by_path(item_id, "SplitLine");
        if nil ~= split_line_ptr then
            split_line_ptr:Show(TRUE);
        end
        WGSelectZoneItem:set_item_or_edit(item_id, game_id, server_info);
        return item_id;
    end;
    
    add_zone_item_impl = function(zone_id)
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(WGSelectZoneList.meunu_id);
        if pMenuControl == nil then
            base_utility.log("[WGSelectZoneList][add_zone_item_impl]pMenuControl is NIL", true);
            return Tenio.INVALID_CONTROLID;
        end 
        local tpf_file = "BannerZone.tpf/TenioUILayout/UIObject/Children/UIObject?CntItem";
        if WGSelectZoneList.LIST_TYPE.FOR_PLUGIN_PAGE == WGSelectZoneList.list_type then
            if WGSelectZoneList.color_mode_ == MainCommon.color_mode.kLight then
                tpf_file = "BannerZoneForPlugin.tpf/TenioUILayout/UIObject/Children/UIObject?CntItem";
            else
                tpf_file = "BannerZoneDarkForPlugin.tpf/TenioUILayout/UIObject/Children/UIObject?CntItem";
            end
        end
        local const_ui_str, str_holder = U8toCUIStr(tpf_file);
        local item_id = pMenuControl:AppendItem(const_ui_str);
        if item_id == Tenio.INVALID_CONTROLID then
            base_utility.log("[WGSelectZoneList][add_zone_item_impl]added item fail, "
                .. " for zone:" .. tostring(zone_id), true);
            return Tenio.INVALID_CONTROLID;
        end
        
        base_utility.log("[WGSelectZoneList][add_zone_item_impl]added item:" .. tostring(item_id)
            .. " for zone:" .. tostring(zone_id));
        WGSelectZoneList.menue_id_to_zone_id[item_id] = zone_id;
        
        return item_id;
    end;
    
    refresh_list = function(game_id)
        base_utility.log("[WGSelectZoneList][refresh_list]enter");
        local servers = banner_model.get_game_servers(game_id);
        if nil == servers then
            base_utility.log("[WGSelectZoneList]refresh_list failed, empty server list", true);
            return;
        end
        if servers.selleted == 0 then
            base_utility.log("[WGSelectZoneList][refresh_list]no selected servers", true);
            local item_id = WGSelectZoneList.add_other_zone_item(game_id, true);
            WGSelectZoneList.other_item_id = item_id;
            ui_utility.setproperty_by_id(item_id, "BackgroundHover", WGSelectZoneList.menu_item_hover_type.hover_updown);
            return nil;
        end
                
        -- 插入大区列表项数量限制
        local server_count = WGSelectZoneList.MAX_SHOW;
        if(servers.server_count < server_count) then
           server_count = servers.server_count;
        end
                
        for i= 0, server_count - 1 do
            local server_info = servers.servers[i];
    
            if server_info ~= nil then
                base_utility.log(string.format("[WGSelectZoneList][refresh_list]set server info %d(%d, %s)",
                                      i, server_info.id, server_info.name));
                local item_id = WGSelectZoneList.add_zone_item(game_id, server_info);
                if i == 0 then
                    ui_utility.setproperty_by_id(item_id, "BackgroundHover", WGSelectZoneList.menu_item_hover_type.hover_up); 
                else
                    ui_utility.setproperty_by_id(item_id, "BackgroundHover", WGSelectZoneList.menu_item_hover_type.hover_mid); 
                end
            else
                base_utility.log(string.format("[WGSelectZoneList][refresh_list]servers.servers[%d] == nil", i), true);
            end;
        end;
        
        local other_item_id = WGSelectZoneList.add_other_zone_item(game_id, false);
        WGSelectZoneList.other_item_id = other_item_id;
        if server_count == 0 then
            ui_utility.setproperty_by_id(other_item_id, "BackgroundHover", WGSelectZoneList.menu_item_hover_type.hover_updown);
        else
            ui_utility.setproperty_by_id(other_item_id, "BackgroundHover", WGSelectZoneList.menu_item_hover_type.hover_down);
        end 
    end;

};

------------------------------------UIEventListener-----------------------------------------------------------------

WGSelectZoneListUIEventListener = {
};

function WGSelectZoneListUIEventListener.on_item_click(uilib, id, item_id, item_index)
     base_utility.log("[WGSelectZoneList][on_item_click]item_id:" .. tostring(item_id));
     local game_id = WGSelectZoneList.current_game_id;
     local zone_id = WGSelectZoneList.menue_id_to_zone_id[item_id];
     base_utility.log("[WGSelectZoneList][on_item_click]zone selected:" .. tostring(zone_id) .. 
                                            ", for game:" .. tostring(game_id));
     
     if game_id == nil or game_id == 0 then
        base_utility.log("[WGSelectZoneList][on_item_click]invalid current game:" ..
             tostring(game_id), true);
         return;
     end
     
     if zone_id ~= nil and zone_id ~= 0 then
          local game_lib =
              base_utility.get_component_adapter("IGame_library",
                  "ierd_tgp.game_library.Get_game_library_adapter();");
          if  game_lib ~= NULL then
              game_lib:set_last_selected_server(game_id, zone_id);
          end;
     else
        local op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_MAIN_WND;
          if WGSelectZoneList.other_item_id ~= nil and
             WGSelectZoneList.other_item_id == item_id then
            op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_FRAME_BOTTOM_ZONE_SELECT_CLICK;
          end;
           -- 大区ID填0，启动游戏即打开选区界面
           -- 网页游戏zone_id = 0 && version="web_game_select_zone",启动游戏即打开选区界面
          local action_info = {
                    game_id_ = game_id,
                    zone_id_ = 0,  -- 大区填0，打开选区界面
                    uid_ = 0,
                    version_ = "web_game_select_zone",
                    launch_op_from = op_from
                };
          
          WGGameAction.excute_action(GameActions.kStartGame, action_info);
    end

    WGSelectZoneList.hide();
end

function WGSelectZoneListUIEventListener.on_show()
    base_utility.log("[WGSelectZoneList][on_show]");
    WGSelectZoneList.fire_zone_list_show(true);
end

function WGSelectZoneListUIEventListener.on_hide()
    base_utility.log("[WGSelectZoneList][on_hide]");
    local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval("WGSelectZoneListUIEventListener.delay_hide_zone_list", 100);
end

function WGSelectZoneListUIEventListener.delay_hide_zone_list()
     WGSelectZoneList.fire_zone_list_show(false);
     local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
     tpfui:RemoveInterval("WGSelectZoneListUIEventListener.delay_hide_zone_list");
end
